$(function () {
    page(1, 10);

    /**
     * 用于选中全部
     */
    $("#checkboxs").click(function () {
        var boxs = $(":input[name='checkbox']")
        for (let i = 0; i < boxs.length; i++) {
            boxs[i].checked = checked
        }
    })

    /**
     * 用于删除选中
     */
    $("#delete_submit").click(function (data) {
        layer.confirm("您确定要删除?", {icon: 3, title: '提示'}, function (e) {
            $.get("wxfun/deleteByIds", $("#tabForm").serialize(), function (data) {
                $(document).ready(page(i, 10))
                window.scrollTo(0, 50);
            })
            layer.close(e)
        });
    })

})

function page(current, pageSize) {
    $.get("wxfun/findFunPage", {currentPage: current, pageSize: pageSize}, function (json) {
        if (json != "index.jsp") {
            if (json.flag) {
                var wxfuns = json.data
                $("#totalPage").html(wxfuns.totalPage)
                $("#totalCount").html(wxfuns.totalCount)
                var wxfun_lis = ''
                for (var i = 0; i < wxfuns.list.length; i++) {
                    var wxfun = wxfuns.list[i]
                    var li = ""
                    li += '<tr>\n' +
                        '        <td><input name="checkbox" value="' + wxfun.id + '" type="checkbox"></td>\n' +
                        '        <td>' + wxfun.fid + '</td>\n' +
                        '        <td><img style="width: 30px;height: 30px" src="' + wxfun.img + '"></td>\n' +
                        '        <td>' + wxfun.drive + '</td>\n' +
                        '        <td>' + wxfun.description + '&nbsp;</td>\n' +
                        '        <td>' + wxfun.flag1 + '&nbsp;</td>\n' +
                        '        <td>\n' +
                        '            <button onclick="updatewxfun(' + wxfun.fid + ',' + wxfuns.currentPage + ')" type="button" class="btn btn-info">修改</button>\n' +
                        '            <button onclick="deletewxfun(' + wxfun.id + ',' + wxfuns.currentPage + ')" type="button" class="btn btn-danger">删除</button>\n' +
                        '        </td>\n' +
                        '    </tr>'
                    wxfun_lis += li
                }
                $("#RTable").html(wxfun_lis)
            }

            var beforeNum = wxfuns.currentPage - 1;
            if (beforeNum <= 0) {
                beforeNum = 1;
            }
            var lis = '<li onclick="javascript:page(' + 1 + ',' + pageSize + ')"><a href="javascript:void(0)">首页</a></li>\n' +
                '<li onclick="javascript:page(' + beforeNum + ',' + pageSize + ')"> <a href="javascript:void(0)" aria-label="Previous"><span aria-hidden="true">&laquo;</span> </a></li>'
            //开始页码
            let begin;
            //结束页码
            let end;
            if (wxfuns.totalPage <= 10) {
                begin = 1;
                end = wxfun.totalPage;
            } else {
                begin = (wxfuns.currentPage - 5);
                end = (wxfuns.currentPage + 4);
                if (begin < 1) {
                    begin = 1;
                    end = begin + 9;
                }
                if (end > wxfun.totalPage) {
                    end = wxfun.totalPage;
                    begin = end - 9;
                }
            }
            for (let i = begin; i <= end; i++) {
                var l = ""
                if (wxfuns.currentPage == i) {
                    l = '<li class="active" onclick="javascript:page(' + i + ',' + pageSize + ')"><a href="javascript:void(0);">' + i + '</a></li>'
                } else {
                    l = '<li onclick="javascript:page(' + i + ',' + pageSize + ')"><a href="javascript:void(0);">' + i + '</a></li>'
                }
                lis += l
            }

            var nextNum = wxfuns.currentPage + 1;
            if (nextNum >= wxfuns.totalPage) {
                nextNum = wxfuns.totalPage;
            }
            lis += '<li onclick="javascript:page(' + nextNum + ',' + pageSize + ')"><a href="javascript:void(0);" aria-label="Next"><span aria-hidden="true">&raquo;</span></a> </li>\n' +
                '<li onclick="javascript:page(' + wxfuns.totalPage + ',' + pageSize + ')"><a href="javascript:void(0);">尾页</a></li>'

            $("#paging").html(lis)
        } else {
            location.href = json
        }
    })
    window.scrollTo(0, 50);

}

/*删除函数*/
function deletewxfun(id, i) {
    layer.confirm("您确定要删除?", {icon: 3, title: '提示'}, function (e) {
        $.get("wxfun/delWxfun", {id:id}, function (json) {
            if (json>0) {
                $(document).ready(page(i, 10))
            }
        })
        layer.close(e)
    });
}

/*更新查询，并放入模态框*/
function updatewxfun(fid, i) {
    $("#modal").html(
        "<div class=\"modal fade\" id=\"updateModal\" tabindex=\"-1\" role=\"dialog\">\n" +
        "    <div class=\"modal-dialog\" role=\"document\">\n" +
        "        <div class=\"modal-content\">\n" +
        "            <div class=\"modal-header\">\n" +
        "                <button type=\"button\" class=\"close\" data-dismiss=\"modal\" aria-label=\"Close\"><span aria-hidden=\"true\">&times;</span></button>\n" +
        "                <h4 class=\"modal-title\" id=\"myModalLabel2\">小程序功能修改</h4>\n" +
        "            </div>\n" +
        "            <div class=\"modal-body\">\n" +
        "                <form id=\"updateFrom\" class=\"form-horizontal\">\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"fid\" class=\"col-sm-2 control-label\">编号：</label>\n" +
        "                        <div class=\"col-sm-10\">\n" +
        "                            <input type=\"text\" name=\"fid\" class=\"form-control\" id=\"fid\" placeholder=\"编号\">\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"img\" class=\"col-sm-2 control-label\">图片：</label>\n" +
        "                        <div class=\"col-sm-10\" style='display: flex;flex-direction: row'>\n" +
        "                            <img id='simg' alt='图片' style='width: 40px;height: 30px;margin-left: 10px'> \n"+
        "                            <input type=\"file\" name=\"img\" class=\"form-control\" id=\"img\" value=\"请选择图片\">\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"drive\" class=\"col-sm-2 control-label\">设备名称：</label>\n" +
        "                        <div class=\"col-sm-10\">\n" +
        "                            <input type=\"text\" name=\"drive\" class=\"form-control\" id=\"drive\" value=\"设备名称\">\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <div class=\"form-group\">\n" +
        "                        <label for=\"description\" class=\"col-sm-2 control-label\">设备描述：</label>\n" +
        "                        <div class=\"col-sm-10\">\n" +
        "                            <input type=\"text\" name=\"description\" class=\"form-control\" id=\"description\" value=\"设备描述\">\n" +
        "                        </div>\n" +
        "                    </div>\n" +
        "                    <input type='text' id='id' name='id' hidden='hidden'> \n "+
        "                </form>\n" +
        "            </div>\n" +
        "            <div class=\"modal-footer\">\n" +
        "                <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>\n" +
        "                <button type=\"button\" id=\"updateSubmit\" class=\"btn btn-primary\">Save</button>\n" +
        "            </div>\n" +
        "        </div>\n" +
        "    </div>\n" +
        "</div>");

    var imgFile = null;
    $.get("wxfun/findbyFid", {fid: fid}, function (json) {
        if (json != "index.jsp") {
            var wxfun = json
            imgFile = wxfun.img;
            $("#simg").attr({src: wxfun.img});
            $("#fid").attr({value: wxfun.fid});
            $("#drive").attr({value: wxfun.drive});
            $("#description").attr({value: wxfun.description});
            $("#id").attr({value: wxfun.id});
        } else {
            location.href = '' + json + ''
        }
    })
    $("#updateModal").modal({
        backdrop: "static"
    })

    $("#updateSubmit").click(function () {
        layer.confirm("您确定要修改?", {icon: 3, title: '提示'}, function (e) {
            var forData = new FormData();
            //获取文件
            var file = $("#updateFrom").find("input")[1].files[0];
            forData.append("fid",$("#fid").val());
            forData.append("drive",$("#drive").val());
            forData.append("description",$("#description").val());
            forData.append("id",$("#id").val());
            if (file == "null" || file == null){
                $.ajax({
                    url:"wxfun/updateWxFunUimg",
                    type:"post",
                    data:forData,
                    contentType:false,
                    processData:false,
                    success:function (data){
                        $(document).ready(page(i, 10))
                    }
                })
            }else {
                forData.append("img",file)
                $.ajax({
                    url:"wxfun/updateWxFun",
                    type:"post",
                    data:forData,
                    contentType:false,
                    processData:false,
                    success:function (data){
                        $(document).ready(page(i, 10))
                    }
                })
            }
            $("#updateModal").modal('hide')
            $(document).ready(page(i, 10))
            layer.close(e)
        })
    })
}
